<template>
  <div>
    <!-- <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
    <el-tab-pane label="必修课程管理" name="first">-->
    <div>
      <div class="search-Box">
        <div class="left-box">
          <div class="item">
            <div class="in-item">
              <span class="label" style>课程编号:</span>
              <el-input size="mini" placeholder="输入课程编号" v-model="mustInfo.tempSearchParam.nid"></el-input>
            </div>
            <div class="in-item">
              <span class="label" style>课程名称:</span>
              <el-input
                size="mini"
                placeholder="输入课程名称"
                v-model="mustInfo.tempSearchParam.coursename"
              ></el-input>
            </div>
          </div>
          <div class="item">
            <div class="in-item">
              <span class="label">教师编号:</span>
              <el-input
                size="mini"
                placeholder="输入教师编号"
                v-model="mustInfo.tempSearchParam.teacherNid"
              ></el-input>
            </div>
            <div class="in-item">
              <span class="label" style>上课班级:</span>
              <el-input
                size="mini"
                placeholder="输入上课班级名称"
                v-model="mustInfo.tempSearchParam.className"
              ></el-input>
            </div>
          </div>
          <div class="item">
            <div class="in-item">
              <span class="label" style>归属学年:</span>
              <el-input size="mini" placeholder="输入归属学年" v-model="mustInfo.tempSearchParam.year"></el-input>
            </div>
            <div class="in-item">
              <span class="label">归属学期:</span>
              <el-input size="mini" placeholder="输入归属学期" v-model="mustInfo.tempSearchParam.xq"></el-input>
            </div>
          </div>
        </div>
        <div class="right-box">
          <div class="item">
            <el-button
              style="margin-left:20px"
              size="mini"
              type="primary"
              icon="el-icon-search"
              @click="mustSearch"
            >搜索</el-button>
            <el-upload
              class="upload"
              ref="upload"
              action="notUse"
              :file-list="mustInfo.mustFileList"
              :auto-upload="false"
              :http-request="mustUploadFile"
              :before-upload="mustBeforeUpload"
              accept=".xls, .xlsx"
              :limit="1"
            >
              <el-button slot="trigger" size="small" type="primary">导入课程列表</el-button>
              <el-button
                style="margin-left: 10px;"
                size="small"
                type="success"
                @click="mustSubmitUpload"
              >上传到服务器</el-button>
              <div slot="tip" class="el-upload__tip">
                <i style="font-style:normal;font-weight:bold">提示</i>:导入课程信息，只能上传xls/xlsx文件，且不超过2mb
              </div>
            </el-upload>
          </div>
        </div>
      </div>
    </div>
    <el-table :data="mustInfo.mustScheList" stripe style="width: 100%;">
      <el-table-column prop="nid" label="课程编号"></el-table-column>
      <el-table-column prop="coursename" label="课程名字"></el-table-column>
      <el-table-column prop="teacher.name" label="任课老师"></el-table-column>
      <el-table-column prop="year" label="所属学年"></el-table-column>
      <el-table-column prop="xq" label="所属学期"></el-table-column>
      <el-table-column prop="address" label="上课地点"></el-table-column>
      <el-table-column prop="time" label="上课时间"></el-table-column>
      <el-table-column prop="class.name" label="上课班级"></el-table-column>
      <el-table-column prop="coursenature" label="课程性质"></el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="checkInfo(scope.$index, scope.row)">查看详细信息</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="foot-box">
      <div style="width:50%">
        <el-button type="success" @click="mustRufush">刷新</el-button>
      </div>
      <div style="width:50%">
        <el-pagination
          background
          :current-page="mustInfo.mustOption.nowPage"
          layout="prev, pager, next"
          :page-size="mustInfo.mustOption.size"
          :total="mustInfo.mustOption.counts"
          @current-change="handleMustCurrentChange"
        ></el-pagination>
      </div>
    </div>

    <!-- </el-tab-pane>
      <el-tab-pane label="选修课程管理" name="second">选修课程管理</el-tab-pane>
    </el-tabs>-->
  </div>
</template>

<script>
import { readExcel, handleMessage, handlePageList } from "../../plugins";
import XLSX from "xlsx";
export default {
  data() {
    return {
      activeName: "first",
      mustInfo: {
        tempSearchParam: {},
        mustScheList: [],
        searchParam: {},
        batchPushList: [],
        mustOption: { size: 10, counts: 0, nowPage: 1 },
        mustFileList: [],
        isSearch: false
      },
      selectableInfo: {}
    };
  },
  methods: {
    handleMustCurrentChange(e) {
      this.mustInfo.mustOption.nowPage = e;
      this.mustInfo.searchParam.page = e;
      if (this.mustInfo.isSearch) {
        this.getSearchMustList();
      } else {
        this.getMustList();
      }
    },
    mustRufush() {
      this.mustInfo.mustOption.nowPage = 1;
      this.mustInfo.searchParam.page = 1;
      this.mustInfo.isSearch = false;
      this.getMustList();
      this.mustInfo.tempSearchParam = {};
    },
    mustSubmitUpload() {
      this.$refs.upload.submit();
    },
    async mustSearch() {
      this.mustInfo.mustOption.nowPage = 1;
      this.mustInfo.isSearch = true;
      this.mustInfo.searchParam.page = 1;
      this.mustInfo.searchParam.nid = this.mustInfo.tempSearchParam.nid;
      this.mustInfo.searchParam.coursename = this.mustInfo.tempSearchParam.coursename;
      this.mustInfo.searchParam.teacherNid = this.mustInfo.tempSearchParam.teacherNid;
      this.mustInfo.searchParam.className = this.mustInfo.tempSearchParam.className;
      this.mustInfo.searchParam.year = this.mustInfo.tempSearchParam.year;
      this.mustInfo.searchParam.xq = this.mustInfo.tempSearchParam.xq;
      this.getSearchMustList();
    },
    async mustUploadFile() {
      //这里发送增加请求，增加多数，后端要写个接口
      // TODO:这里添加请求，发送过去的数据就是json格式的batchPushList
      console.log(this.mustInfo.batchPushList);
      const loading = this.$loading({
        lock: true,
        text: "正在上传数据中，请稍等~",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      let res = await this.$http.post(
        "inerstMore/Course",
        this.mustInfo.batchPushList
      );
      loading.close();
      if (res.data.success) {
        this.$message.success(res.data.message);
      } else {
        this.$message.error(res.data.message);
      }
      this.getMustList();
      this.mustInfo.mustFileList = [];
    },
    mustBeforeUpload(file) {
      let _this = this;
      //使返回的值变成Promise对象，如果校验不通过，则reject，校验通过，则resolve
      return new Promise(function(resolve, reject) {
        readExcel(_this, file, XLSX, "mustSchedule").then(
          res => {
            const isLt2M = file.size / 1024 / 1204 < 2;
            if (!isLt2M) {
              _this.$message.error("文件大小不能超过2M！");
            }
            if (res && isLt2M) resolve(true);
            reject(false);
          },
          error => {
            //此处为reject返回时候
            _this.$message.error(error);
            reject(false);
          }
        );
      });
    },
    checkInfo(index, row) {
      this.$router.push(`/manager/scheduleEdit/${row._id}`);
    },
    async handleDelete(index, row) {
      this.$confirm(`是否确定要删除 "${row.coursename}"`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        let res = await this.$http.delete(`rest/Course/${row._id}`);
        handleMessage(this, res.data);
        if (this.mustInfo.isSearch) {
          this.getSearchMustList();
        } else {
          this.getMustList();
        }
      });
    },
    async getSearchMustList() {
      let res = await this.$http.get("searchList/Course", {
        params: this.mustInfo.searchParam
      });
      if (res.data.success) {
        this.mustInfo.mustScheList = res.data.list;
        this.mustInfo.mustOption.counts = res.data.counts;
      } else {
        this.$message.error(res.data.message);
      }
    },
    getMustList() {
      let _this = this;
      return new Promise(async function(resolve, reject) {
        let res = await _this.$http.get("list/Course",{ params: _this.mustInfo.searchParam });
        if (res.data.success) {
          _this.mustInfo.mustScheList = res.data.list;
          _this.mustInfo.mustOption.counts = res.data.counts;
        } else {
          _this.$message.error(res.data.message);
        }

        resolve(true);
      });
    }
  },
  created() {
    const loading = this.$loading({
      lock: true,
      text: "数据加载中，请稍等~",
      spinner: "el-icon-loading",
      background: "rgba(0, 0, 0, 0.7)"
    });
    Promise.all([this.getMustList()]).then(res => {
      loading.close();
    });
  }
};
</script>

<style scoped>
.tap {
  display: flex;
}
.upload {
  margin-left: 50px;
}
.search-Box {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  width: 100%;
  /* margin-left: 100px; */
  justify-content: space-between;
}
.left-box {
  width: 50%;
}
.right-box {
  width: 50%;
}
.search-Box .item {
  margin-top: 20px;
  display: flex;
  align-items: center;
}
.search-Box .item:nth-child(1) {
  margin-top: 0;
}
.search-Box .in-item {
  display: flex;
  align-items: center;
}

.search-Box .in-item {
  margin-left: 50px;
}
.search-Box .in-item:nth-child(1) {
  margin: 0;
}
.in-item .label {
  width: 100px;
  font-size: 14px;
  text-align: right;
  margin-right: 10px;
}
.foot-box {
  display: flex;
  margin-top: 20px;
  align-items: flex-end;
  padding: 0 50px;
  padding-right: 80px;
  justify-content: space-between;
}
</style>